<?PHP
	require 'utils.php';
	require 'config.php';
	
	ini_set('display_errors', 1);
	
	SessionExist();	
	//Main functions, compares UIDs from Cookies & DB, returns "true" if equal
	function SessionExist()
	{		
		//SetCookieLive('key1', '303031');
		//SetCookieLive('key2', '404041');
		$vResult = false;
		$vIDsFromIP = getIDFromIP();
		$vIDFromCookies = getIDFromCookies();
		if(count($vIDsFromIP) > 0 and intval($vIDFromCookies) > 0)
		{
			foreach ($vIDsFromIP as $vIDbyIP)
			{
				if($vIDbyIP == $vIDFromCookies)
				{
					Trace('Selected ID = ' . $vIDbyIP);					
					$vResult = true;
					break;
				}			
			}
		}
		return $vResult;
	}
	
	function getUserID()
	{
		$vResult = -1;
		$vIDFromIP = getIDFromIP();
		$vIDFromCookies = getIDFromCookies();
		if(intval($vIDFromIP) > 0 and intval($vIDFromCookies) > 0)
		{			
			if($vIDFromIP == $vIDFromCookies)
			{				
				$vResult = $vIDFromIP;
				break;
			}			
		}
		return $vResult;
	}
	
	//Getting UID from DB using users IP
	function getIDFromIP()
	{
		$vLink = ConnectToDB();	
		$vIPCondition = getIPCondition();			
		$vQuery = "SELECT user_id FROM dikar2011_session WHERE " . $vIPCondition;
		echo $vQuery . '</br>';
		$vResults = mysql_query($vQuery);
		$vResult = array();		
		while($vLine = mysql_fetch_assoc($vResults))
		{		
			$vResult[] = $vLine['user_id'];			
		}
		
		echo 'User IDs by IP: ';
		print_r($vResult);
		echo '</br>';		
		
		mysql_close($vLink);
		
		return $vResult;
	}
	//Function perfom ip extraction and return SQL query based on extracted IP
	function getIPCondition()
	{
		$ClientIP = $_SERVER['REMOTE_ADDR'];
		$vResult = array();
		$vResult = explode('.', $ClientIP);
		Trace('User IP: ' . $ClientIP);
		return "ipa=" . $vResult[0] . " AND ipb=" . $vResult[1] . " AND ipc=" . $vResult[2] . " AND ipd=" . $vResult[3];
	}
	//Function provide Cookies chek & SQL query execution with cookie-delivered keys
	function getIDFromCookies()
	{
		$vLink = ConnectToDB();
		$vKey1 = -1;
		$vKey2 = -1;
		if (isset($_COOKIE['key1']))
			$vKey1 = $_COOKIE['key1'];
		if (isset($_COOKIE['key2']))
			$vKey2 = $_COOKIE['key2'];
		Trace('Keys in cookies: ' . $vKey1 . ', ' . $vKey2);
		
		$vCondition = "key1=" . $vKey1 . " AND key2=" . $vKey2;
		$vQuery = "SELECT id FROM dikar2011_obj WHERE " . $vCondition;		
		$vResult = -1;
		$vResults = mysql_query($vQuery);		
		while($vLine = mysql_fetch_assoc($vResults))			
			$vResult = $vLine['id'];
		Trace('User ID by cookies: ' . $vResult);
		
		mysql_close($vLink);
		
		return $vResult;
	}
	
	function ConnectToDB()
	{
		global $DBServer, $DBUser, $DBPassword, $DBName;
		$vLink = mysql_connect($DBServer, $DBUser, $DBPassword);
		if(!$vLink) 
			die(mysql_error());
		//setting session encoding
		mysql_select_db($DBName, $vLink);	
		mysql_query("SET NAMES 'cp1251';", $vLink);
		mysql_query("SET CHARACTER SET 'cp1251';", $vLink);
		mysql_query("SET SESSION collation_connection = 'cp1251_general_ci';", $vLink);
		return $vLink;
	}
?>